package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import pojo.Book;
import pojo.Order;
import util.DBUtil;
import dao.OrderDao;

public class OrderDaoImpl implements OrderDao {

	Connection conn;
	PreparedStatement ps;
	
	@Override
	public int addOrder(Order order) {
		conn = getConnection();
		String sql = "insert into orders(id, uuid, bid, bcount) values(?,?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ps.setString(1, (order.getId() != null) ? order.getId() : UUID
					.randomUUID().toString());
			ps.setString(2, order.getUid());
			ps.setString(3, order.getBid());
			ps.setInt(4, order.getCount());
			
			int m = ps.executeUpdate();
			conn.commit();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public int updateOrder(Order order) {
		conn = getConnection();
		String sql = "update orders set uuid=?,bid=?,bcount=? where id=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ps.setString(1, order.getUid());
			ps.setString(2, order.getBid());
			ps.setInt(3, order.getCount());
			ps.setString(4, order.getId());
			
			int m = ps.executeUpdate();
			conn.commit();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public List<Order> findAllOrders() {
		conn = getConnection();
		List<Order> list = new ArrayList<Order>();
		String sql = "select * from orders";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ResultSet set = ps.executeQuery();
			while (set.next()) {
				Order order = new Order();
				order.setId(set.getString("id"));
				order.setUid(set.getString("uuid"));
				order.setBid(set.getString("bid"));
				order.setCount(set.getInt("bcount"));
				list.add(order);
			}
			conn.commit();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return list;
		}
	}

	@Override
	public int addAllOrders(List<Order> orders) {
		conn = getConnection();
		String sql = "insert into orders(id, uuid, bid, bcount) values(?,?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			if(orders==null){
				return 0;
			}
			
			for(Order order:orders){
				ps.setString(1, (order.getId() != null) ? order.getId() : UUID
						.randomUUID().toString());
				ps.setString(2, order.getUid());
				ps.setString(3, order.getBid());
				ps.setInt(4, order.getCount());
				ps.addBatch();
			}
			int[] ms = ps.executeBatch();
			
			for(int i:ms){
				if(i!=-2){
					try {
						conn.rollback();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					return 0;
				}
			}
			conn.commit();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public int deleteOrder(String orderId) {
		conn = getConnection();
		String sql = "delete orders where id=?";
		try {
			ps = conn.prepareStatement(sql);

			ps.setString(1,orderId);
			int m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Order searchOrder(String orderId) {
		conn = getConnection();
		String sql = "select * from orders where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, orderId);
			ResultSet set = ps.executeQuery();
			if (set.next()) {
				Order order = new Order();
				order.setId(set.getString("id"));
				order.setUid(set.getString("uuid"));
				order.setBid(set.getString("bid"));
				order.setCount(set.getInt("bcount"));
				return order;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();

			return null;
		}
	}

	private Connection getConnection() {
		try {
			InitialContext cxt = new InitialContext();// / JNDI 树
			DataSource ds = (DataSource) cxt
					.lookup("java:/comp/env/jdbc/myoracle");// // 查找 [tomcat
															// 协议]/jdbc/xxxx
			Connection conn = ds.getConnection();
			// System.out.println(conn);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		return DBUtil.createConnection();
	}
	
}
